package com.whz.workmanageol.controller;

import com.github.pagehelper.PageInfo;
import com.whz.workmanageol.entity.Course;
import com.whz.workmanageol.entity.Grade;
import com.whz.workmanageol.entity.Student;
import com.whz.workmanageol.entity.Teacher;
import com.whz.workmanageol.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    // 教师信息管理部分
    @GetMapping(value = "/teacherinfo")
    public String teacherinfo(@RequestParam(required = false, defaultValue = "0") Integer pageNum,Model model){
        PageInfo<Teacher> teacherPageInfo = adminService.teacherList(pageNum);
        model.addAttribute("teacherPageInfo", teacherPageInfo);
        return "bootstrap/teacherinfo";
    }

    @PostMapping(value = "updateTeacherInfo")
    public String updateTeacherInfo(Teacher teacher, Model model){
        adminService.updateTeacherInfo(teacher);
        teacherinfo(0, model);
        // 重新放入教师列表
        return "bootstrap/index";
    }
    @ResponseBody
    @PostMapping(value = "delTeacherInfo")
    public void delTeacherInfo(String teacherid, Model model){
        adminService.delTeacherInfo(teacherid);
        // 重新放入教师列表
        teacherinfo(0, model);
    }

    @PostMapping(value = "seachteacher")
    public String seachteacher(String teacherid, Model model){
        // 搜索结果放入教师列表
        List<Teacher> teacherList = adminService.searchTeacher(teacherid);
        PageInfo<Teacher> teacherPageInfo = new PageInfo<>(teacherList);
        model.addAttribute("teacherPageInfo", teacherPageInfo);
        return "bootstrap/teacherinfo";
    }

    // 学生信息管理部分
    @GetMapping(value = "/studentinfo")
    public String studentinfo(@RequestParam(required = false, defaultValue = "0") Integer pageNum,Model model){
        PageInfo<Student> studentPageInfo = adminService.studentList(pageNum);
        model.addAttribute("studentPageInfo", studentPageInfo);
        return "bootstrap/studentinfo";
    }

    @PostMapping(value = "delStudentInfo")
    public String delStudentInfo(String studentid, Model model){
        adminService.delStudentInfo(studentid);
        // 重新放入学生列表
        PageInfo<Student> studentPageInfo = adminService.studentList(1);
        model.addAttribute("studentPageInfo", studentPageInfo);
        return "bootstrap/studentinfo";
    }

    @PostMapping(value = "updateStudentInfo")
    public String updateStudentInfo(Student student, Model model){
        adminService.updateStudentInfo(student);
        // 重新放入学生列表
        PageInfo<Student> studentPageInfo = adminService.studentList(1);
        model.addAttribute("studentPageInfo", studentPageInfo);
        return "bootstrap/studentinfo";
    }

    @PostMapping(value = "seachStudent")
    public String seachStudent(String studentid, Model model){
        // 搜索结果放入学生列表
        List<Student> studentList = adminService.searchStudent(studentid);
        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        model.addAttribute("studentPageInfo", studentPageInfo);
        return "bootstrap/studentinfo";
    }

    // 班级信息管理
    @GetMapping(value = "/gradeInfo")
    public String gradeInfo(@RequestParam(required = false, defaultValue = "0") Integer pageNum,Model model){
        PageInfo<Grade> gradePageInfo = adminService.gradeList(pageNum);
        model.addAttribute("gradePageInfo", gradePageInfo);
        return "bootstrap/gradeinfo";
    }
    @ResponseBody
    @PostMapping(value = "delGradeInfo")
    public void delGradeInfo(String gradeid, Model model){
        adminService.delGradeInfo(gradeid);
        gradeInfo(0, model);
    }

    @PostMapping(value = "updateGradeInfo")
    public String updateGradeInfo(Grade grade, Model model){
        adminService.updateGradeInfo(grade);
        gradeInfo(0, model);
        return "bootstrap/gradeinfo";
    }
    @PostMapping(value = "seachGrade")
    public String seachGrade(String gradename, Model model){
        List<Grade> gradeList = adminService.searchGrade(gradename);
        PageInfo<Grade> gradePageInfo = new PageInfo<>(gradeList);
        model.addAttribute("gradePageInfo", gradePageInfo);
        return "bootstrap/gradeinfo";
    }

    // 课程信息管理
    @GetMapping(value = "/courseInfo")
    public String courseInfo(@RequestParam(required = false, defaultValue = "0") Integer pageNum,Model model){
        PageInfo<Course> coursePageInfo = adminService.courseList(pageNum);
        model.addAttribute("coursePageInfo", coursePageInfo);
        return "bootstrap/courseinfo";
    }
    @ResponseBody
    @PostMapping(value = "delCourseInfo")
    public void delCourseInfo(String courseid, Model model){
        adminService.delCourseInfo(courseid);
        // 重新放入教师列表
        courseInfo(0, model);
    }

    @PostMapping(value = "updateCourseInfo")
    public String updateCourseInfo(Course course, Model model){
        adminService.updateCourseInfo(course);
        // 重新放入教师列表
        courseInfo(0, model);
        return "bootstrap/courseinfo";
    }
    @PostMapping(value = "seachCourse")
    public String seachCourse(String coursename, Model model){
        // 搜索结果放入教师列表
        List<Course> courseList = adminService.searchCourse(coursename);
        PageInfo<Course> coursePageInfo = new PageInfo<>(courseList);
        model.addAttribute("coursePageInfo", coursePageInfo);
        return "bootstrap/courseinfo";
    }

    @GetMapping(value = "importInfo")
    public String importInfo(){
        return "bootstrap/importinfo";
    }


}
